package model;
import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

import model.base.BaseStation;

@SuppressWarnings("serial")
public class Station extends BaseStation<Station> {
    public static final Station dao = new Station().dao();
    
    public List<Record> findMarkers(){
        return Db.find(" SELECT tc.id , tc.name AS title , tc.latitude, tc.longitude FROM t_station tc WHERE tc.isDelete = 0  " );
    }
    
	public Station findFirstByNameAndCompanyId(String name , Long companyId){
        return this.findFirst(" SELECT ts.* FROM t_station ts WHERE ts.name = ? AND ts.companyId = ?  LIMIT 1 " , name , companyId);
    }

	public boolean isRepeatName(){
		Station ex = Station.dao.findFirstByNameAndCompanyId( getName() , getCompanyId() );
		return ( null == getId() && null != ex) || (null != ex && !ex.getId().equals(getId()));
	}
	
	public void removeByServicerId(Long servicerId) {
		Db.update(" UPDATE t_station ts  LEFT JOIN t_staff tsf ON tsf.id = ts.staffId SET ts.staffId = ? WHERE tsf.servicerId = ?  " , null , servicerId );
	}

	public Station findFirstByStatusAndCompanyId(Integer status , Long companyId){
        return this.findFirst(" SELECT ts.* FROM t_station ts WHERE ts.status = ? AND ts.companyId = ?  LIMIT 1 " , status , companyId);
    }
}
